package com.smarth.zhizulian.http.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 网络状态检测工具类，需要权限 <uses-permission
 * android:name="android.permission.ACCESS_NETWORK_STATE"/>
 *
 * @author luckchoudog
 */
public class NetConnectUtils {
    public static Context mContext;

    public static void init(Context context) {
        mContext = context;
    }

    /**
     * 判断网络连接状态
     *
     * @return true表示有网络链接，false表示没有网络连接
     */
    public static boolean isNetworkConnected() {
        if (mContext != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext
                    .getSystemService(mContext.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断wifi状态
     *
     * @return true表示wifi处于开启状态，false表示wifi处于关闭状态
     */
    public static boolean isWifiConnected() {
        if (mContext != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo.State state = mConnectivityManager.getNetworkInfo(
                    ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
            if (NetworkInfo.State.CONNECTED == state) { // 判断是否正在使用WIFI网络
                return true;
            }
        }
        return false;
    }

    /**
     * 判断移动网络
     *
     * @return true表示已经开启移动网络，false表示没有开启移动网络连接
     */
    public static boolean isMobileConnected() {
        if (mContext != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo.State statestate = mConnectivityManager.getNetworkInfo(
                    ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
            if (NetworkInfo.State.CONNECTED == statestate) { // 判断是否正在使用GPRS网络3G网络
                return true;
            }
        }
        return false;
    }
}
